<?php

namespace App\HttpController\AuthLogin;

use App\Utility\Auth\Wx\WxAuth;
use EasySwoole\Core\Http\AbstractInterface\Controller;

class WxLogin extends Controller {
	private $config = [ 
			'app_key' => 'wxced6c089dfd379be',
			'app_secret' => '73bd918322f0f504263ee4c603316b1f',
			'scope' => 'snsapi_userinfo',
			'grant_type' => '',
			'callback' => 'http://chat.7b620.cn/AuthLogin/WxLogin/callback',
			'response_type' => '' 
	];
	
	/**
	 *
	 * {@inheritdoc}
	 *
	 * @see \EasySwoole\Core\Http\AbstractInterface\Controller::index()
	 */
	public function index() {
		// TODO Auto-generated method stub
	}
	private function getOauth(): WxAuth {
		return new WxAuth ( $this->config );
	}
	function login() {
		$url = $this->getOauth ()->get_code ();
		$this->response ()->redirect ( $url );
	}
	function callback() {
		$req = $this->request ()->getRequestParam ();
		$this->response ()->withHeader ( 'Content-type', 'text/html; charset=UTF-8;' );
		$acc = $this->getOauth ()->get_access_token ( $req ['code'], $req ['state'] );
		$userinfo = $this->getOauth ()->get_user_info ( $acc );
		var_dump ( $userinfo->toArray () );
	}
	protected function onException(\Throwable $throwable, $actionName): void {
		$this->response ()->write ( $actionName . "onException" );
	}
}